package com.xdf.xzymanagementsystem.config;

import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfigure implements WebMvcConfigurer {
    // 客户合同
    @Value("${uploadfile.contractclient.virtualpath}")
    private String contractClientVirtualpath;

    @Value("${uploadfile.contractclient.filepath}")
    private String contractClientFilePath;

    // 供应商合同
    @Value("${uploadfile.contractsupplier.virtualpath}")
    private String contractSupplierVirtualpath;

    @Value("${uploadfile.contractsupplier.filepath}")
    private String contractSupplierFilePath;

    @Value("${globalv.virtualpath}")
    private String virtualpath;

    @Value("${globalv.drawingpath}")
    private String drawingpath;

    @Value("${uploadfile.qc.virtualpath}")
    private String virtualpathQc;

    @Value("${uploadfile.qc.filepath}")
    private String filepathQc;

    /**
     * 使用 MDC 实现链路跟踪
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LogInterceptor()).addPathPatterns("/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(contractClientVirtualpath).addResourceLocations("file:/" + contractClientFilePath);
        registry.addResourceHandler(contractSupplierVirtualpath).addResourceLocations("file:/" + contractSupplierFilePath);
//        registry.addResourceHandler("/drawing/**").addResourceLocations("file:E:/drawingfiles/");
        registry.addResourceHandler(virtualpath).addResourceLocations("file:/" + drawingpath);
        registry.addResourceHandler(virtualpathQc).addResourceLocations("file:/" + filepathQc);
    }

    @Bean
    public TomcatServletWebServerFactory webServerFactory() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
        factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
            @Override
            public void customize(Connector connector) {
                connector.setProperty("relaxedPathChars", "\"<>[\\]^`{|}");
                connector.setProperty("relaxedQueryChars", "\"<>[\\]^`{|}");
            }
        });
        return factory;
    }
}
